package com.hl.toolkit.server.tycoin;

import com.tycoin.common.enums.MediaStatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class AppGlobal {
	public static final String P_PARTNER_PROVIDER_ID = "providerId";
	
	public static String profiles;

	public static String webAccessUrl;

	public static String partnerBackUrl;

	public static String imageWebPath;

	public static String getProfiles() {
		return profiles;
	}

	@Value("${spring.profiles.active:dev}")
	public void setProfiles(String profiles) {
		AppGlobal.profiles = profiles;
	}

	public static String getWebAccessUrl() {
		return webAccessUrl;
	}

	@Value("${web.access.url:}")
	public void setWebAccessUrl(String webAccessUrl) {
		AppGlobal.webAccessUrl = webAccessUrl;
	}

	public static String getPartnerBackUrl() {
		return partnerBackUrl;
	}

	@Value("${partner.backUrl:}")
	public void setPartnerBackUrl(String partnerBackUrl) {
		AppGlobal.partnerBackUrl = partnerBackUrl;
	}

	public static String getImageWebPath() {
		return imageWebPath;
	}

	@Value("${image.web.path:}")
	public void setImageWebPath(String imageWebPath) {
		AppGlobal.imageWebPath = imageWebPath;
	}

	public static String getImagePath(String path) {
		if (StringUtils.isEmpty(path)) {
			return "";
		}
		if (path.indexOf("http") == 0) {
			return path;
		}
		return StringUtils.trimToEmpty(getImageWebPath())
				+ StringUtils.trimToEmpty(path);
	}

	/**
	 * 缩进字符串
	 * 
	 * @param str
	 * @param maxWidth
	 * @return
	 */
	public static String abbreviate(String str, int maxWidth) {
		if (str == null) {
			return "";
		}
		if (str.length() <= maxWidth) {
			return str;
		}
		final String abrevMarker = "...";
		return str.substring(0, maxWidth - 1) + abrevMarker;
	}

	/**
	 * 截取字符
	 * 
	 * @param source
	 * @param maxWidth
	 * @return
	 */
	public static String cutCharacter(String source, int maxWidth) {
		String sources = source.trim();
		if (sources == null) {
			return "";
		} else if (sources.length() > maxWidth) {
			sources = source.substring(0, maxWidth);
		}
		return sources;
	}

	/**
	 * 缩进字符串and 换行
	 * 
	 * @param source
	 * @param maxWidth
	 * @return
	 */
	public static String reviateWrapSpace(String source, int maxWidth) {
		if (source == null) {
			return "";
		}
		String result = abbreviate(source, maxWidth).replace("\r\n",
				"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		return result;
	}

	// 赶考标清的课程
	public static String reviateWrapSpacesd(String source, int maxWidth) {
		if (source == null) {
			return "";
		}
		String result = abbreviate(source, maxWidth).replace("\r\n", "<br/>");
		return result;
	}

	// 赶考标清的新闻
	public static String reviateWrapSpacesdNews(String source, int maxWidth) {
		if (source == null) {
			return "";
		}
		String result = abbreviate(source, maxWidth).replace("\r\n", "<br/>");
		return result;
	}

	public static String getMediaStatusDesc(int mediaStatus) {
		StringBuffer sb = new StringBuffer();
		if (mediaStatus == MediaStatusEnum.OK.getKey()) {
			sb.append("<span class=\"badge badge-success\">")
					.append(MediaStatusEnum.OK.getValue()).append("</span>");
			return sb.toString();
		}

		if (isMediaStatusOpen(mediaStatus, MediaStatusEnum.DOWNLOAD.getKey())) {
			sb.append("<span class=\"badge badge-info\">")
					.append(MediaStatusEnum.DOWNLOAD.getValue())
					.append("</span>");
		}

		if (isMediaStatusOpen(mediaStatus,
				MediaStatusEnum.DOWNLOAD_FAIL.getKey())) {
			sb.append("<span class=\"badge badge-danger\">")
					.append(MediaStatusEnum.DOWNLOAD_FAIL.getValue())
					.append("</span>");
		}

		if (isMediaStatusOpen(mediaStatus,
				MediaStatusEnum.DOWNLOAD_PART_FAIL.getKey())) {
			sb.append("<span class=\"badge badge-danger\">")
					.append(MediaStatusEnum.DOWNLOAD_PART_FAIL.getValue())
					.append("</span>");
		}

		if (isMediaStatusOpen(mediaStatus, MediaStatusEnum.MISS_VIDEO.getKey())) {
			sb.append("<span class=\"badge badge-danger\">")
					.append(MediaStatusEnum.MISS_VIDEO.getValue())
					.append("</span>");
		}

		if (isMediaStatusOpen(mediaStatus, MediaStatusEnum.MISS_IMAGE.getKey())) {
			sb.append("<span class=\"badge badge-danger\">")
					.append(MediaStatusEnum.MISS_IMAGE.getValue())
					.append("</span>");
		}

		return sb.toString();
	}

	public static boolean isMediaStatusOpen(int mediaStatus, int mask) {
		if (mask <= 0) {
			return false;
		}
		int index = (mediaStatus / mask);
		if (index % 10 == 1) {
			return true;
		}
		return false;
	}

	public static int setMediaStatusOpen(int mediaStatus, int... masks) {
		int result = mediaStatus;
		for (int mask : masks) {
			if (!isMediaStatusOpen(result, mask)) {
				result += mask;
			}
		}
		return result;
	}

	public static int setMediaStatusClose(int mediaStatus, int... masks) {
		int result = mediaStatus;
		for (int mask : masks) {
			if (isMediaStatusOpen(result, mask)) {
				result -= mask;
			}
		}
		return result;
	}

	public static boolean isStatusOpen(int status, int mask) {
		if (mask <= 0) {
			return false;
		}
		int index = (status / mask);
		if (index % 10 == 1) {
			return true;
		}
		return false;
	}

	public static int setStatusOpen(int status, int... masks) {
		int result = status;
		for (int mask : masks) {
			if (!isStatusOpen(result, mask)) {
				result += mask;
			}
		}
		return result;
	}

	public static int setStatusClose(int status, int... masks) {
		int result = status;
		for (int mask : masks) {
			if (isStatusOpen(result, mask)) {
				result -= mask;
			}
		}
		return result;
	}

}
